213 IX: RPi.GPIO for Simple Raspberry Pi 5 Programs (213.html)

Keywords

ICH180RR RPi5GPIO RPi.GPIO gpiozero "Raspberry Pi 5" "1-character conversion" "BroadCom GPIO Pin Numbers" GPIO.BCM Python Thonny "Cytron Maker pHAT" "Maker pHAT"

/KeywordsEnd

(To enlarge .....Click it)
thumb: IXimage.jpg
IX or (IX by DC) or "|><"


This article is a part of the IX family of software.

Introduction

********   RPi.GPIO   for   Raspberry Pi 5B   ********


Raspberry Pi 5B computers cannot use RPi.GPIO to control simple GPIO pins. Users must import and use gpiozero instead, entailing extensive code conversions. The author of this website has developed an easy method to painlessly convert simple RPi.GPIO code into gpiozero code. This method is useful when converting very simple Python programs that use RPi.GPIO for use on Raspberry Pi 5B computers. Initial testing shows that Python conversion to control GPIO pins on the RPi 5B, requires changing as little as 1 character in the code.     ONLY ONE CHARACTER!!

Conversion From RPi.GPIO To gpiozero

The simple Python program shown below uses "import RPi.GPIO . ." to control the GPIO pins. This program can be easily converted to use "import RPi5GPIO . ." to control the GPIO pins on a Raspberry Pi 5B (using gpiozero in RPi5GPIO) as shown below:

                            Simply replace the "." with a "5"
#Test_RPi5GPIO_D.py
#By D@CC on 2024AJan18
#Purpose: Control GPIO using RPi.GPIO code on a RPi 5B
#Source: https://ephotocaption.com/a/213/213.html
import time
PINout=12                                
#import RPi.GPIO as GPIO    #Deprecated  #                 v
import  RPi5GPIO as GPIO                 #  ->   import RPi5GPIO as GPIO          
GPIO.setmode(GPIO.BCM)                   #                 ^
GPIO.setup(PINout,GPIO.OUT)
while True:
    GPIO.output(PINout,True)  #turn LED on
    time.sleep(1)
    GPIO.output(PINout,False) #turn LED off
    time.sleep(1)
#/Test_RPi5GPIO_D.py
BEWARE: the version of RPi5GPIO.py in Source 02 will NOT handle physical GPIO pin numbers.

RPi5GPIO Issue(s)

ISSUE 01 - as of 2024AJan19 Any statements testing for GPIO.HIGH (or GPIO.LOW) can simply be buffered with the unity() function.
    This is really a minor issue because GPIO.input(pin) can be compared to True (not to GPIO.HIGH).

RPi.GPIO vs gpiozero

If a Cytron Maker pHAT (Source 06) is connected to the GPIO pins on a RPi 5B, the LED on GPIO 12 will be controlled by the Test_RPi5GPIO_D.py code in Source 03.

One might wonder which (of RPi.GPIO or gpiozero) is being used when the source code differences are so minimal. One sure way (to distinguish between the two) is to run the above program twice under Thonny on a RPi 4. When RPi.GPIO is being used, Thonny will produce the warning: "Run Time Warning: This channel is already in use." When gpiozero is being used (via the "import RPi5GPIO as GPIO"). Thonny will produce no such warning. In both cases, the LED will be blinking.

On an RPi 5B, the use of RPi.GPIO will produce the error message: "Run Time Error: cannot determine SOC peripheral Base Address". On an RPi 5B, when gpiozero is used (via the "import RPi5GPIO as GPIO" statement) the program will run successfully, the LED on the Cytron Maker pHAT will be blinking. The Raspberry Pi 5B will be blinking the LED even though the Python code "seems" to be using RPi.GPIO statements.

Simply changing one character on the " import RPi.GPIO as GPIO" statement makes the former code (written for RPi.GPIO) work on the Raspberry Pi 5B. It is, of course, the "." in RPi.GPIO that must be changed to a "5".

On the Raspberry Pi 5B, this program can be run in the Terminal using Python with the same results. To function correctly in Terminal mode on the RPi 5B, it is only necessary to "import RPi5GPIO as GPIO" for this program to run. Of course, Source 02 containing RPi5GPIO_py.txt must be downloaded into the folder containing the program that will import it. It will also be necessary to rename RPi5GPIO_py.txt to RPi5GPIO.py . The reason that RPi5GPIO.py is downloaded as RPi5GPIO_py.txt is because any Raspberry computer will immediately run any code using the extension ".py" that is downloaded.

NOTE: As of 2024AJan19, the RPi5GPIO.py code controls LEDs and push-buttons on the GPIO pins.

Source 01 (G) has been modified to also control a LED using a pushButton. It has been tested on Raspberry Pi computers.
Source 02 (containing the latest RPi5GPIO library) works well. It has been tested on various Raspberry Pi computers.

Compute Module 4 GPIO Pin Numbers

The author has recently discovered that the Compute Module 4 only recognizes BroadCom GPIO Pin numbers (not physical GPIO numbers). Therefore the author will NOT be supporting Physical GPIO Pin numbers in the RPi5GPIO.py code. Users must convert their code so that it uses BroadCom GPIO Pin numbers.

Overheating the Raspberry Pi 5B

Anyone concerning with overheating the RPi 5B can check its temperature using:
	>$ usr/bin/vcgencmd measure_temp
	>$ temp=64.2'C

	or

	>$ cd Desktop/IX_assets
	>:~Desktop/IX_assets $ sh CPU_temp.sh
	>$ temp=64.2'C
	
The Raspberry Pi 5B will automatically throttle the clock speed making an effort to keep the CPU temperature below 85'C. It will also make best use of any available fan or active cooler. To date the author is using a 4GB RPi 5B and SSD using a feeble power supply (designed for an RPi 4). Minor computing activities, to date, have put no stress on this little power supply, temperatures remaining below 70°C. Pinouts for the RPi 5B can be found in Source 05.

Sources

Video Sources

Video Source V213:01: FINALLY! NVMe SSDs on the Raspberry Pi (13:30m) by Jeff Geerling c 2023 K Nov 16

Web Sources

Web Source S213:01:www Test_RPi5GPIO_G.py (as text) by D@CC on 2024AJan18
Web Source S213:02:www RPi5GPIO.py (as text) by D@CC on 2024AJan19
Web Source S213:03:www Test_RPi5GPIO_D.py (as text) by D@CC on 2024AJan18
Web Source S213:04:www gpiozero - migrating from RPI.GPIO by gpiozero read the docs on 2024AJan19
Web Source S213:05:www pinout.ai raspberry-pi-5 by FLEETSTACK.IO on 2024AJan19
Web Source S213:06:www Cytron Maker pHAT by Cytron as of 2024AJan19

/SourcesEnd


There is a way to "google" any of the part-numbers, words or phrases in all my articles. This "google-like" search limits itself ONLY to my articles. Just go to the top of "Articles by Old King Cole" and look for the "search" input box named "freefind".

Click here to return to Articles by Old King Cole

Date Created:2024 A Jan 17
Last Updated:2024 A Jan 19

All rights reserved 2024 by © ICH180RR

saved in E:\E\2022\DevE\MyPagesE\Globat\ePhotoCaption.com\a\213\213.html
backed up to ePhotoCaption.com\a\213\213_2024AJan19.html

Font: Courier New 10 (monospaced)
/213.html